package com.likersai.opon.config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * @author sun.ye
 * @since 2020-11-27 10:02
 */
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    //定义授权规则
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //super.configure(http);
        //定制请求的授权规则
        http.csrf().disable();
        http.cors().disable();
        http.authorizeRequests().antMatchers("/**").permitAll();
        //开启自动配置的登录功能，如果没有权限，就会来到登录页面
//        http.formLogin();
        //1、/login来到登录页
        //2、重定向到/login?error表示登录失败
        //3、更多详细规定
    }

    //定义认证规则
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //super.configure(auth);
        //这里本来应该查询数据库的，但是emmm
        auth.inMemoryAuthentication().withUser("zhangsan").password("123456").roles("VIP1","VIP2")
                .and().withUser("lisi").password("123456").roles("VIP2","VIP3")
                .and().withUser("wangwu").password("123456").roles("VIP3","VIP1");

    }



}